#Persistent
#SingleInstance Force
U_GVIM := "C:\Program Files\Vim\vim72\gvim.exe"


applicationname=Lock Screen Appinn V1.4

IfExist, Lock.ico
   Menu, tray, icon, Lock.ico

IniRead, idle, ahk_setting.ini, lock, idle, 10
IniRead, key, ahk_setting.ini, lock, key, appinn
IniRead, interface, ahk_setting.ini, lock, interface, 1
IniRead, direct_lock, ahk_setting.ini, lock, direct_lock, 0
IniRead, if_turnoff_monitor, ahk_setting.ini, lock, if_turnoff_monitor, 0
IniRead, if_fullscreen, ahk_setting.ini, lock, if_fullscreen, 0


IniRead, hotkey, ahk_setting.ini, lock, hotkey, #k
Hotkey, %hotkey%, start, On               ;Turn on the hotkey.

IniRead, language, ahk_setting.ini, lock, language, 0
Gosub, setLang

SetTimer,CheckIdle,10000

Gosub, makeMenu
; optimize memory
EmptyMem()

if ( direct_lock = 1 )
{
   Gosub, start
}
return

setPassword:
   InputBox, new_key, password, , HIDE, 130, 100
   if NOT ErrorLevel
   {
      IniWrite, %new_key%, ahk_setting.ini, lock, key
      key := new_key
   }
return

setAutoLock:
   InputBox, new_idle, the idle time(minutes), , , 240, 100
   if NOT ErrorLevel
   {
      IniWrite, %new_idle%, ahk_setting.ini, lock, idle
      idle := new_idle
   }
return

start:
   SetTimer, CheckIdle, Off
   SystemCursor(0)
   BlockInput, MouseMove
   if ( interface = 1 )
   {
      Gui, +AlwaysOnTop +Disabled -SysMenu +Owner -Caption +ToolWindow
      if ( if_fullscreen = 1 )
      {
         ww := A_ScreenWidth // 2 - 65
         hh := A_Screenheight // 2 - 15
         Gui, Add, text, x%ww% y%hh%, %L_enterpw1%`n%L_enterpw2%
         CustomColor = 999A9B
         Gui, Color, %CustomColor%
         Gui, Show, Maximize
      }
      else
      {
         Gui, Add, text, , %L_enterpw1%`n%L_enterpw2%
         CustomColor = 999A9B
         Gui, Color, %CustomColor%
         Gui, Show
      }
   }
   if ( interface = 2 )
   {
      Gui, +AlwaysOnTop +Disabled -SysMenu +Owner -Caption +ToolWindow
      if ( if_fullscreen = 1 )
      {
         Gui, margin, 0,0
         Gui, Add, Picture, w%A_ScreenWidth% h%A_ScreenHeight%, %A_ScriptDir%\lock.jpg
         CustomColor = EE0000
         Gui, Color, %CustomColor%
         Gui, Show, Maximize
      }
      else
      {
         Gui, Add, Picture, w300 h-1, %A_ScriptDir%\lock.jpg
         CustomColor = EE0000
         Gui, Color, %CustomColor%
         Gui, Show
      }
   }
   if ( if_turnoff_monitor = 1 )
   {
      SetTimer, TurnoffMonitor, 3000 
      StringRight, H_key, hotkey, 1
      KeyWait %H_key%
      KeyWait LWin
      KeyWait RWin
      KeyWait Shift
      KeyWait Control
      KeyWait Alt
      ;Power off the screen
      SendMessage, 0x112, 0xF170, 2,,ahk_id 0xFFFF
   }
   Hotkey, Lbutton, stop, on
   Hotkey, Rbutton, stop, on
   Hotkey, Mbutton, stop, on
   Hotkey, LWin, stop, on
   Hotkey, Rwin, stop, on
   Hotkey, LAlt, stop, on
   Hotkey, RAlt, stop, on
   Hotkey, Ctrl, stop, on
   Hotkey, esc, stop, on
   Hotkey, del, stop, on
   Hotkey, f1, stop, on
   Hotkey, f4, stop, on
   Hotkey, tab, stop, on
   i:=1
   Loop
   {
      Input, a, L1
      StringLeft, temp, key, %i%
      StringRight, temp, temp, 1
      if a=%temp%
      {
         i++
      }else{
         i:=1
      }
      if (i=(strlen(key)+1))
      {
         if ( interface = 1 )
         {
            Gui, Destroy
            if ( if_fullscreen = 1 )
            {
            }
            else
            {
               Gui, +AlwaysOnTop +Disabled -SysMenu +Owner -Caption +ToolWindow
               Gui, Add, text, , %L_unlock%
               CustomColor = 999A9B
               Gui, Color, %CustomColor%
               Gui, Show
               sleep, 300
               Gui, Destroy
            }
         }
         if ( interface = 2 )
         {
            Gui, Destroy
            if ( if_fullscreen = 1 )
            {
            }
            else
            {
               Gui, +AlwaysOnTop +Disabled -SysMenu +Owner -Caption +ToolWindow
               Gui, Add, Picture, w300 h-1, %A_ScriptDir%\lock.jpg
               CustomColor = 00EE00
               Gui, Color, %CustomColor%
               Gui, Show
               sleep, 300
               Gui, Destroy
            }
         }
         if ( if_turnoff_monitor = 1 )
         {
            SetTimer, TurnoffMonitor, off 
         }
         SystemCursor(1)

         BlockInput, MouseMoveOff
         Hotkey, Lbutton, stop, off
         Hotkey, Rbutton, stop, off
         Hotkey, Mbutton, stop, off
         Hotkey, LWin, stop, off
         Hotkey, Rwin, stop, off
         Hotkey, LAlt, stop, off
         Hotkey, RAlt, stop, off
         Hotkey, Ctrl, stop, off
         Hotkey, esc, stop, off
         Hotkey, del, stop, off
         Hotkey, f1, stop, off
         Hotkey, f4, stop, off
         Hotkey, tab, stop, off

         SetTimer, CheckIdle, On
         return
      }
   }
return

stop:
return

makeMenu:
   Menu, tray, NoStandard
   Menu, tray, DeleteAll
   Menu, tray, Add, %L_lock%, start
   Menu, tray, Default, %L_lock%
   Menu, tray, Add, %L_about%..., about
   Menu, tray, Add, %L_donate%..., donate
   Menu, tray, Add
   Menu, setting, Add, %L_password%, setPassword
   Menu, setting, Add, %L_autolock%, setAutoLock
   Menu, setting, Add, %L_hotkey%, changeHotkey
   Menu, setting, Add
   Menu, setting, Add, %L_direct_lock%, setDirectLock
   if ( direct_lock = 1 )
   {
      Menu, setting, Check, %L_direct_lock%
   }
   else
   {
      Menu, setting, UnCheck, %L_direct_lock%
   }
   Menu, tray, Add, %L_setting%, :setting
   IfExist, lock.lng
   {
      menu, language, Add, %L_english%, lang0
      menu, language, Add, %L_simple%, lang1
      menu, language, Add, %L_traditional%, lang2
      Menu, language, UnCheck, %L_english%
      Menu, language, UnCheck, %L_simple%
      Menu, language, UnCheck, %L_traditional%
      if ( language = 0 ) 
         Menu, language, Check, %L_english%
      else if ( language = 1 ) 
         Menu, language, Check, %L_simple%
      else if ( language = 2 ) 
         Menu, language, Check, %L_traditional%

      Menu, tray, Add, %L_language%, :language
   }
   menu, interface, Add, %L_interface1%, interface1
   menu, interface, Add, %L_interface2%, interface2
   menu, interface, Add, %L_interface3%, interface3
   Menu, interface, Add
   Menu, interface, Add, %L_fullscreen%, setFullscreen
   if ( if_fullscreen = 1 )
   {
      Menu, interface, Check, %L_fullscreen%
   }
   else
   {
      Menu, interface, UnCheck, %L_fullscreen%
   }
   Menu, interface, Add, %L_screenoff%, setTurnoffMonitor
   if ( if_turnoff_monitor = 1 )
   {
      Menu, interface, Check, %L_screenoff%
   }
   else
   {
      Menu, interface, UnCheck, %L_screenoff%
   }
   menu, interface, UnCheck, %L_interface1%
   menu, interface, UnCheck, %L_interface2%
   menu, interface, UnCheck, %L_interface3%
   if ( interface = 1 )
      menu, interface, Check, %L_interface1%
   else if ( interface = 2 )
      menu, interface, Check, %L_interface2%
   else if ( interface = 3 )
      menu, interface, Check, %L_interface3%

   Menu, tray, Add, %L_interface%, :interface
   Menu, tray, Add
   Menu, tray, Add, Reload Script, reloadScript
   Menu, tray, Add, Edit Script, editScript
   Menu, tray, Add, %L_exit%, exitit
return

setFullscreen:
   if ( if_fullscreen = 1 )
   {
      if_fullscreen = 0
      Menu, interface, UnCheck, %L_fullscreen%
   }
   else
   {
      if_fullscreen = 1
      Menu, interface, Check, %L_fullscreen%
   }
   IniWrite, %if_fullscreen%, ahk_setting.ini, lock, if_fullscreen
return

setTurnoffMonitor:
   if ( if_turnoff_monitor = 1 )
   {
      if_turnoff_monitor = 0
      Menu, interface, UnCheck, %L_screenoff%
   }
   else
   {
      if_turnoff_monitor = 1
      Menu, interface, Check, %L_screenoff%
   }
   IniWrite, %if_turnoff_monitor%, ahk_setting.ini, lock, if_turnoff_monitor
return

setDirectLock:
   if ( direct_lock = 1 )
   {
      direct_lock = 0
      Menu, setting, UnCheck, %L_direct_lock%
   }
   else
   {
      direct_lock = 1
      Menu, setting, Check, %L_direct_lock%
   }
   IniWrite, %direct_lock%, ahk_setting.ini, lock, direct_lock
return

setLang:
   IfExist lock.lng
   {
      FileRead, lang, lock.lng
      loop, parse, lang, `n
      {
         if ( A_Index >= 1 * (language + 1) && A_Index <= 28 * (language + 1) )
         {      
         A_lang_i := Mod(A_Index, 28)
         lang_%A_lang_i% := UtfDecode(A_LoopField)
         }
         if ( A_Index = 0 * 28 + 2)
            L_english := UtfDecode(A_LoopField)
         if ( A_Index = 1 * 28 + 2)
            L_simple := UtfDecode(A_LoopField)
         if ( A_Index = 2 * 28 + 2)
            L_traditional := UtfDecode(A_LoopField)
      }
      L_lock     := lang_3
      L_about    := lang_4
      L_password := lang_5
      L_autolock := lang_6
      L_hotkey   := lang_7
      L_direct_lock := lang_8
      L_setting  := lang_9
      L_language := lang_10
      L_interface:= lang_11
      L_exit     := lang_12
      L_autolocktime := lang_13
      L_enterpw1 := lang_14
      L_enterpw2 := lang_15
      L_unlock   := lang_16
      L_invalid_hotkey := lang_17
      L_donate   := lang_18
      L_applicationfunction := lang_19
      L_applicationtip := lang_20
      L_authors := lang_21
      L_purpose := lang_22
      L_from := lang_23
      L_interface1 := lang_24
      L_interface2 := lang_25
      L_interface3 := lang_26
      L_fullscreen := lang_27
      L_screenoff := lang_0
   }
   else
   {
      L_english := "English"
      L_simple := "Simple Chinese"
      L_traditional := "Triditional Chinese"
      L_lock     := "Lock"
      L_about    := "About"
      L_password := "Password"
      L_autolock := "Auto lock"
      L_hotkey   := "Hotkey"
      L_direct_lock := "Lock at start"
      L_setting  := "Setting"
      L_language := "Language"
      L_interface:= "Interface"
      L_exit     := "Exit"
      L_autolocktime := "the idle time(minutes)"
      L_enterpw1  := "You have locked your keyboard and mouse"
      L_enterpw2  := "Please input the correct password to unlock"
      L_unlock   := "Unlocked"
      L_invalid_hotkey := "Invalid Hotkey!"
      L_donate    := "Donate"
      L_applicationfunction := "Lock the screen, and input password to unlock."
      L_applicationtip := "Double click the TrayIcon to lock the screen at once." 
      L_authors := "Appinn Software - Liang Zhuge, Willin Tang and sfufoet"
      L_purpose := "Share the free, small, practical, interesting and green softwares."
      L_from := "This tool is made by AutoHotKey"
      L_interface1 := "1"
      L_interface2 := "2"
      L_interface3 := "3"
      L_fullscreen := "Full screen"
      L_screenoff := "Turn off monitor"
   }
return

lang0:
   language = 0
   IniWrite, 0, ahk_setting.ini, lock, language
   Menu, setting, DeleteAll
   Menu, interface, DeleteAll
   Gosub setLang
   Gosub makeMenu
return

lang1:
   language = 1
   IniWrite, 1, ahk_setting.ini, lock, language
   Menu, setting, DeleteAll
   Menu, interface, DeleteAll
   Gosub setLang
   Gosub makeMenu
return

lang2:
   language = 2
   IniWrite, 2, ahk_setting.ini, lock, language
   Menu, setting, DeleteAll
   Menu, interface, DeleteAll
   Gosub setLang
   Gosub makeMenu
return

interface1:
   interface = 1
   IniWrite, 1, ahk_setting.ini, lock, interface
   Menu, setting, DeleteAll
   Menu, interface, DeleteAll
   Gosub makeMenu
return

interface2:
   interface = 2
   IniWrite, 2, ahk_setting.ini, lock, interface
   Menu, setting, DeleteAll
   Menu, interface, DeleteAll
   Gosub makeMenu
return

interface3:
   interface = 3
   IniWrite, 3, ahk_setting.ini, lock, interface
   Menu, setting, DeleteAll
   Menu, interface, DeleteAll
   Gosub makeMenu
return

reloadScript:
   Reload
return

editScript:
   run, %U_GVIM% %A_ScriptFullPath%
return

donate:
run https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=664Y63YBVZ4Y6&lc=C2&item_name=Appinn`%2ecom&item_number=lock`%2dscreen`%2dappinn&currency_code=USD&bn=PP`%2dDonationsBF`%3abtn_donateCC_LG`%2egif`%3aNonHosted
return

About:
Gui,99:Destroy
Gui,99:Margin,15,15
Gui,99:Add,Picture,xm, Appinn.com.png
Gui,99:Font,Bold
Gui,99:Add,Text,xm y+20,%A_space%%A_space%%applicationname%
Gui,99:Font
Gui,99:Add,Text,x25 y+10,%L_applicationfunction%
Gui,99:Add,Text,x25 y+5,%L_applicationtip%

Gui,99:Font,Bold
Gui,99:Add,Text,xm y+20,%A_space%%A_space%by %L_authors%
Gui,99:Font
Gui,99:Add,Text,x25 y+10,%L_purpose%
Gui,99:Font,CBlue Underline
Gui,99:Add,Text,x25 y+5 gAppinn,www.appinn.com
Gui,99:Font

Gui,99:Font,Bold
Gui,99:Add,Text,xm y+20,%A_space%%A_space%Powered by AutoHotkey
Gui,99:Font
Gui,99:Add,Text,x25 y+10,%L_from%
Gui,99:Font,CBlue Underline
Gui,99:Add,Text,x25 y+5 gAUTOHOTKEY,www.AutoHotkey.com
Gui,99:Add,Text,x25 y+5 gAppinnAhk,www.appinn.com/category/autohotkey/
Gui,99:Font

Gui,99:Show,,About %applicationname% 
hCurs:=DllCall("LoadCursor","UInt",NULL,"Int",32649,"UInt")
OnMessage(0x200,"WM_MOUSEMOVE")
Return

Appinn:
  Run,http://www.appinn.com,,UseErrorLevel
Return

AUTOHOTKEY:
  Run,http://www.autohotkey.com,,UseErrorLevel
Return

AppinnAhk:
  Run,http://www.appinn.com/category/autohotkey/,,UseErrorLevel
return

99GuiClose:
  Gui,99:Destroy
  OnMessage(0x200,"")
  DllCall("DestroyCursor","Uint",hCur)
Return

WM_MOUSEMOVE(wParam,lParam)
{
  Global hCurs
  MouseGetPos,,,,ctrl
  If ctrl in Static7,Static10,Static11
    DllCall("SetCursor","UInt",hCurs)
  Return
}
Return

exitit:
   ExitApp
return

EmptyMem(PID="AHK Rocks"){
    pid:=(pid="AHK Rocks") ? DllCall("GetCurrentProcessId") : pid
    h:=DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
    DllCall("SetProcessWorkingSetSize", "UInt", h, "Int", -1, "Int", -1)
    DllCall("CloseHandle", "Int", h)
}

CheckIdle:
   if ( idle > 0 )
   {
      idleTime:=idle*60*1000
      if ( A_TimeIdle >= idleTime )
      {
         Gosub start
      }
   }
return

SystemCursor(OnOff=1)   ; INIT = "I","Init"; OFF = 0,"Off"; TOGGLE = -1,"T","Toggle"; ON = others
{
   static AndMask, XorMask, $, h_cursor
      , c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 ; system cursors
      ,    b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13   ; blank cursors
      ,    h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13   ; handles of default cursors
   if (OnOff = "Init" or OnOff = "I" or $ = "")      ; init when requested or at first call
   {
      $ = h                                ; active default cursors
      VarSetCapacity( h_cursor,4444, 1 )
      VarSetCapacity( AndMask, 32*4, 0xFF )
      VarSetCapacity( XorMask, 32*4, 0 )
      system_cursors = 32512,32513,32514,32515,32516,32642,32643,32644,32645,32646,32648,32649,32650
      StringSplit c, system_cursors, `,
      Loop %c0%
      {
         h_cursor   := DllCall( "LoadCursor", "uint",0, "uint",c%A_Index% )
         h%A_Index% := DllCall( "CopyImage",  "uint",h_cursor, "uint",2, "int",0, "int",0, "uint",0 )
         b%A_Index% := DllCall("CreateCursor","uint",0, "int",0, "int",0
            , "int",32, "int",32, "uint",&AndMask, "uint",&XorMask )
      }
   }
   if (OnOff = 0 or OnOff = "Off" or $ = "h" and (OnOff < 0 or OnOff = "Toggle" or OnOff = "T"))
      $ = b  ; use blank cursors
   else
      $ = h  ; use the saved cursors

   Loop %c0%
   {
      h_cursor := DllCall( "CopyImage", "uint",%$%%A_Index%, "uint",2, "int",0, "int",0, "uint",0 )
      DllCall( "SetSystemCursor", "uint",h_cursor, "uint",c%A_Index% )
   }
}

changeHotkey:
   StringLeft, old_Win, hotkey, 1 
   StringTrimLeft, old_Hotkey, hotkey, 1 
   Gui, -SysMenu
   if (old_Win = "#")
   {
      Gui, Add, Hotkey,   vHK, %old_Hotkey%     ;add a hotkey control
      Gui, Add, CheckBox, vCB x+5 hp Checked, Win  ;add a checkbox to allow the Windows key (#) as a modifier.
   }
   else
   {
      Gui, Add, Hotkey,   vHK, %hotkey%     ;add a hotkey control
      Gui, Add, CheckBox, vCB x+5 hp , Win  ;add a checkbox to allow the Windows key (#) as a modifier.
   }
   Gui, Add, Button, default ys, OK  
   Gui, Add, Button, ys, Cancel  
   Gui, Show,,hotkey
return

ButtonOK:
   Gui, Submit, NoHide
   If CB                                  ;If the 'Win' box is checked, then add its modifier (#).
   {
      HK := "#" HK
   }
   FoundPos := RegExMatch(HK, "^[#\+\^!]+\w$")
   if ( FoundPos = 1 )
   {
      Hotkey, %hotkey%, start, off
      hotkey := HK
      Hotkey, %hotkey%, start, on
      IniWrite, %hotkey%, ahk_setting.ini, lock, hotkey
   }
   else
   {
      MsgBox, %L_invalid_hotkey%
   }
   Gui, Destroy
return

ButtonCancel:
   Gui, Submit, NoHide
   Gui, Destroy
return

UtfDecode( str ) {
  RawLen := StrLen(str)
  
  Charset := 0    ; Put 1252 or 0

  BufSize := (RawLen + 1) * 2
  VarSetCapacity(Buf, BufSize, 0)

  DllCall("MultiByteToWideChar", "uint", 65001, "int", 0, "str", str
                               , "int", -1, "uint", &Buf, "uint", RawLen + 1)
  DllCall("WideCharToMultiByte", "uint", Charset, "int", 0, "uint", &Buf, "int", -1
                               , "str", str, "uint", RawLen + 1
                               , "int", 0, "int", 0)
  Return str
}

TurnoffMonitor:
      if ( A_TimeIdle >= 5000 )
      {
         ;Power off the screen
         SendMessage, 0x112, 0xF170, 2,,ahk_id 0xFFFF
      }
return

